//
//  SettingsView.swift
//  EasyTranslate
//
//  Created by 周波 on 2024/5/29.
//

import SwiftUI

struct SettingsView: View {
    @State private var isPrivacy = false
    @State private var isHelp = false
    @State private var isService = false
    @Environment(\.presentationMode) var presentationMode
    
    var body: some View {
        
        NavigationView {
            ZStack {
                MainView()
                VStack (spacing:0) {
                    
                    Ba(title: "Setting"){
                        self.presentationMode.wrappedValue.dismiss()
                    }
                    
                    ZStack {
                        VStack(spacing: 0) {
                            ScrollView {
                                VStack(spacing: 0) {
                                    ListRow(id: 1, title: "Privacy Policy", imageName: "privacy")
                                        .onTapGesture {
                                            self.isPrivacy.toggle()
                                        }
                                    
                                    ListRow(id: 3, title: "Terms of Service", imageName: "service")
                                        .onTapGesture {
                                            self.isService.toggle()
                                        }
                                    ListRow(id: 4, title: "Tell Others", imageName: "tell")
                                        .onTapGesture {
                                            shareText("Search for QuickBooster - VPN Proxy Master in App Store")
                                        }
                              
                                ListRow(id: 2, title: "About", imageName: "content")
                                    .onTapGesture {
                                        self.isHelp.toggle()
                                    }    .onTapGesture {
                                        isHelp.toggle()
                                    }.sheet(isPresented: $isHelp, content: {
                                      About()
                                    })

                                
                                Spacer()
                                }
                            }
                        }
                        
                        NavigationLink(destination: Privacy(), isActive: $isPrivacy) {
                            EmptyView()
                        }
                        .hidden()
                       
                        NavigationLink(destination: Service(), isActive: $isService) {
                            EmptyView()
                        }
                        .hidden()
                    }
                    
                    
                    
                    
                    Spacer()
                }
                
                .navigationBarHidden(true)
                .navigationBarBackButtonHidden(true)
            }
            
        }.navigationBarHidden(true)
            .navigationBarBackButtonHidden(true)
            .navigationViewStyle(StackNavigationViewStyle())
    }
    func shareText(_ text: String) {
        guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
              let rootViewController = windowScene.windows.first?.rootViewController else {
            return
        }
        
        let activityViewController = UIActivityViewController(activityItems: [text], applicationActivities: nil)
        
        // 在 iPad 上设置 sourceView
        if let popoverController = activityViewController.popoverPresentationController {
            popoverController.sourceView = rootViewController.view
            popoverController.sourceRect = CGRect(x: UIScreen.main.bounds.width / 2, y: UIScreen.main.bounds.height / 2, width: 0, height: 0)
            popoverController.permittedArrowDirections = []
        }
        
        rootViewController.present(activityViewController, animated: true, completion: nil)
    }
}

#Preview {
    SettingsView()
}


struct ListRow: View {
    var id: Int
    var title: String
    var imageName: String
    
    var body: some View {
        HStack {
            Image(imageName)
                .resizable()
                .frame(width: 26, height: 26)
            Text(title)
                .font(.headline)
                .foregroundColor(.white)
            Spacer()
            Image(systemName: "chevron.right")
                .foregroundColor(.gray)
                .padding(.trailing, 10)
        }
        .padding(10)
       
        .cornerRadius(10)
        .shadow(radius: 5)
        .padding(.leading, 20)
        .padding(.trailing,20)
        .padding(.top,20)
        
       
    }
}
